package com.wlyuan.open.domain.source.dispatcher.conversion;

import com.wlyuan.core.exception.AssertUtils;
import com.wlyuan.core.exception.ValueError;
import com.wlyuan.open.dto.OpenGoodsSyncDTO;
import com.wlyuan.open.utils.LongUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.stereotype.Service;

/**
 * @author lijing
 * @description
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class OpenDispatcherGoodsConversion implements OpenDispatcherConversion<OpenGoodsSyncDTO> {
    private final BaseOpenDispatcherConversion baseOpenDispatcherConversion;

    @Override
    public void convert(OpenGoodsSyncDTO source) {
        var tenant = source.getTenantId();
        if (LongUtils.notNullLong(tenant)) {
            var tenantId = baseOpenDispatcherConversion.tenant(tenant);
            AssertUtils.assertNotNull(tenantId, new ValueError("OpenGoodsSyncDTO租户不存在:" + tenant), source);
            source.setTenantId(tenantId);
        }
        //创建人
        var createBy = source.getCreateBy();
        if (LongUtils.notNullLong(createBy)) {
            Long user = baseOpenDispatcherConversion.user(createBy);
            AssertUtils.assertNotNull(user, new ValueError("OpenGoodsSyncDTO创建人不存在:" + createBy), source);
            source.setCreateBy(user);
        }
        //修改人
        var updateBy = source.getUpdateBy();
        if (LongUtils.notNullLong(updateBy)) {
            Long user = baseOpenDispatcherConversion.user(updateBy);
            AssertUtils.assertNotNull(user, new ValueError("OpenGoodsSyncDTO修改人不存在:" + updateBy), source);
            source.setUpdateBy(user);
        }
    }
}
